<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="824.48">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; min-height: 15.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
span.s1 {color: #0000bf}
span.s2 {color: #000000}
span.Apple-tab-span {white-space:pre}
</style>
</head>
<body>
<p class="p1"><b>PV_MagSmooth<span class="Apple-tab-span">	</span><span class="Apple-tab-span">	</span>Smooth spectral magnitudes over time</b></p>
<p class="p2"><br></p>
<p class="p3"><span class="Apple-tab-span">	</span><b>chain = PV_MagSmooth(chain, factor)</b></p>
<p class="p2"><br></p>
<p class="p4">Smooths out the magnitude of FFT bins over time using <i>recursive averaging</i>, according to <b>factor</b> which varies from 0 (no smoothing occurs) to 1 ("infinite" smoothing, magnitudes never change).</p>
<p class="p5"><br></p>
<p class="p4">For each bin, the calculation looks like:</p>
<p class="p4"><span class="Apple-tab-span">	</span>mag = (prevmag * factor) + (mag * (1-factor))</p>
<p class="p5"><br></p>
<p class="p4"><b>Example</b></p>
<p class="p2"><br></p>
<p class="p6">s.boot;</p>
<p class="p6">b = <span class="s1">Buffer</span>.alloc(s, 1024);</p>
<p class="p7"><span class="s2">c = </span><span class="s1">Buffer</span><span class="s2">.read(s, </span>"sounds/a11wlk01.wav"<span class="s2">);</span></p>
<p class="p8"><br></p>
<p class="p6">(</p>
<p class="p6">x = {</p>
<p class="p6"><span class="s1">var</span> son, chain, out;</p>
<p class="p8"><br></p>
<p class="p6">son = <span class="s1">PlayBuf</span>.ar(1, c, loop: 1);</p>
<p class="p8"><br></p>
<p class="p6">chain = <span class="s1">FFT</span>(b, son);</p>
<p class="p6">chain = <span class="s1">PV_MagSmooth</span>(chain, 1 - <span class="s1">MouseX</span>.kr(1, 0.00001, 1));</p>
<p class="p8"><br></p>
<p class="p6">out = <span class="s1">IFFT</span>(chain);</p>
<p class="p8"><br></p>
<p class="p6">(out * 0.3).dup</p>
<p class="p8"><br></p>
<p class="p6">}.play;</p>
<p class="p6">)</p>
<p class="p6">x.free;</p>
<p class="p8"><br></p>
<p class="p9">// This one subtracts the smoothed version away, to leave just the spiky bits!</p>
<p class="p9">// This is a fairly well-known basis of noise-removal by spectral subtraction,</p>
<p class="p9">// which works well if the noise is static or slow-changing while the signal<span class="Apple-converted-space"> </span></p>
<p class="p9">// is fast-changing.</p>
<p class="p9">// In this demo, mouse left/right controls the amount of smoothing,</p>
<p class="p9">//<span class="Apple-converted-space">  </span>and when the mousebutton is down you hear the "original"<span class="Apple-converted-space"> </span></p>
<p class="p9">//<span class="Apple-converted-space">  </span>(otherwise you hear the "cleaned" version).</p>
<p class="p6">d = <span class="s1">Buffer</span>.alloc(s, 1024);</p>
<p class="p6">(</p>
<p class="p6">x = {</p>
<p class="p6"><span class="s1">var</span> son, chain, chainorig, chainsmooth, out;</p>
<p class="p8"><br></p>
<p class="p6">son = <span class="s1">PlayBuf</span>.ar(1, c, loop: 1);</p>
<p class="p8"><br></p>
<p class="p6">chain = <span class="s1">FFT</span>(b, son);</p>
<p class="p6">chainorig = <span class="s1">PV_Copy</span>(chain, d);</p>
<p class="p6">chainsmooth = <span class="s1">PV_MagSmooth</span>(chain, 1 - <span class="s1">MouseX</span>.kr(1, 0.00001, 1));</p>
<p class="p6">chain = <span class="s1">PV_MagSubtract</span>(chainorig, chainsmooth, 1);</p>
<p class="p8"><br></p>
<p class="p6">out = <span class="s1">XFade2</span>.ar(<span class="s1">IFFT</span>(chain), son, <span class="s1">MouseButton</span>.kr(-1,1));</p>
<p class="p8"><br></p>
<p class="p6">(out * 0.3).dup</p>
<p class="p8"><br></p>
<p class="p6">}.play;</p>
<p class="p6">)</p>
<p class="p6">x.free;</p>
<p class="p8"><br></p>
<p class="p8"><br></p>
<p class="p8"><br></p>
<p class="p6">b.free; c.free;</p>
</body>
</html>
